/**
 * 公共方法
 */
let base = {}
base.install = function (Vue, options) {
  var _this = Vue.prototype;
  var Common = Vue.prototype.$common = {};

  /**
   * 在新页面打开
   * @param {*} $this
   * @param {*} routeName
   * @param {*} param 参数对象，对象格式
   */
  Common.OpenNewPage = function ($this, routePath, param) {
    let routeData = $this.$router.resolve({
      path: routePath,
      query: param
    });
    window.open(routeData.href, '_blank');
  }
  /**
   * 写入sessionStorage
   */
  Common.setSessionStorage = function (key, objData) {
    if (typeof objData == 'string') {
      sessionStorage.setItem(key, objData);
    } else {
      sessionStorage.setItem(key, JSON.stringify(objData));
    }

  }
  /**
   * 读取sessionStorage
   */
  Common.getSessionStorage = function (key, isObj) {
      var data = sessionStorage.getItem(key);
      if (isObj) {
        return JSON.parse(data);
      }
      return data;
    },
    /**
     * 移除sessionStorage
     */
    Common.removeSessionStorage = function (key) {
      sessionStorage.removeItem(key);
    },
    Common.timestampToTime = function (row, column) {
      /**
       * 
       * 用法：
          <el-table-column
            :formatter="this.$common.timestampToTime"
            align="center"
            prop="createTime"
            label="创建时间"
          >
          
          </el-table-column>
       */
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    Common.toDate = function (timestamp) {
      /**
       * 
       * 用法：
          <el-table-column
            :formatter="this.$common.toDate"
            align="center"
            prop="createTime"
            label="创建时间"
          >
          
          </el-table-column>
       */
      if (timestamp == undefined) {
        return "";
      }
      return moment(timestamp).format("YYYY-MM-DD");
    },
    Common.isSuccess = function (data, callback) {
      /**
       * 用法
       * 
            apis.msgApi
              .deleteBatch({ ids: ids })
              .then((data) => {
                this.$common.isSuccess(data, () => {
                  this.onSearch()
                })
              })
              .catch((err) => {
                this.$message({ message: '执行失败，请重试', type: 'error' })
              })
       */
      if (data && data.data) {
        var json = data.data;
        if (json.status == 'SUCCESS') {
          _this.$message({
            message: '执行成功',
            type: "success"
          });
          callback(json);
        } else if (json.message) {
          _this.$message({
            message: json.message,
            type: "error"
          });
        }
      } else {
        _this.$message({
          message: '执行异常，请重试',
          type: "error"
        });
      }
    },
    // cookie储蓄
    Common.setCookie = function (cname, cvalue, exdays) {
      let d = new Date()
      d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000))
      let expires = 'expires=' + d.toUTCString()
      document.cookie = cname + '=' + cvalue + '; ' + expires
    },
    Common.getCookie = function (cname) {
      let name = cname + '='
      let ca = document.cookie.split(';')
      // console.log("获取cookie,现在循环")
      for (let i = 0; i < ca.length; i++) {
        let c = ca[i]
        // console.log(c)
        while (c.charAt(0) === ' ') c = c.substring(1)
        if (c.indexOf(name) !== -1) {
          return c.substring(name.length, c.length)
        }
      }
      return ''
    },
    Common.clearCookie = function (cname) {
      this.setCookie(cname, '', -1)
    }
}

export default base
